package com.titar.robot.http;


import java.io.IOException;

import retrofit2.Response;

public class ApiResponse<T> {
    public static <T> ApiErrorResponse<T> create(Throwable error) {
        return new ApiErrorResponse(error);
    }

    public static <T> ApiResponse<T> create(Response<T> response) {
        if (response.isSuccessful()) {
            T body = response.body();
            if (body == null || response.code() == 204) {
                return new ApiEmptyResponse();
            } else {
                return new ApiSuccessResponse(body, "");
            }
        } else {
            String msg = "";
            if (response.errorBody() != null) {
                try {
                    msg = response.errorBody().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String errorMsg = "";
            if (msg.isEmpty()) {
                errorMsg = response.message();
            } else {
                errorMsg = msg;
            }

            return new ApiErrorResponse(errorMsg != "" ? errorMsg : "unkonw error");
        }
    }

}







